package cn.uikit.demo.vp

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import cn.uikit.demo.databinding.FragmentRecyclerViewBinding
import cn.uikit.demo.databinding.ItemRecyclerContentBinding
import sivin.uikit.adapter2.CommonAdapter
import sivin.uikit.adapter2.ItemViewDelegate
import sivin.uikit.adapter2.XViewHolder
import sivin.uikit.framework.BindingFragment


class RecyclerFragment :
    BindingFragment<FragmentRecyclerViewBinding>(FragmentRecyclerViewBinding::inflate) {
    private val dataList by lazy {
        val list = mutableListOf<String>()
        for (i in 1..30) {
            list.add("数学:题目 $i")
        }
        list
    }


    private val rvAdapter by lazy {
        val adapter = CommonAdapter<String>()
        adapter.addItemViewDelegate(object :
            ItemViewDelegate<String, ItemRecyclerContentBinding>(
                ItemRecyclerContentBinding::inflate,
            ) {
            override fun onBindData(
                holder: XViewHolder<ItemRecyclerContentBinding>,
                item: String,
                position: Int
            ) {
                holder.binding.title.text = item
            }

            override fun isForViewType(item: String, position: Int): Boolean {
                return true
            }
        })
        adapter.setDataList(dataList)
        adapter
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val layoutManager = LinearLayoutManager(context)
        binding.rv.layoutManager = layoutManager
        binding.rv.adapter = rvAdapter
//        layoutManager.scrollToPositionWithOffset(90, 0)
    }
}